package com.manager.plat.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigFilterDao;
import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.vo.ConfigFilterMapVo;
import com.manager.plat.vo.ConfigFilterVo;
import com.manager.plat.vo.ConfigLowerPartnerVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class ConfigFilterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 804402081159382695L;

	private static Logger logger = Logger.getLogger(ConfigFilterAction.class);
	
	private IConfigFilterDao configFilterDao;					//业务过滤数据接口
	private IConfigLowerPartnerDao configLowerPartnerDao;		//渠道信息数据接口
	private List<ConfigFilterMapVo> configFilterMapVoList;		//渠道扣量组合信息列表
	private List<ConfigLowerPartnerVo> configLowerPartnerVoList;//渠道信息列表
	private ConfigFilterVo configFilterVo;						//业务过滤信息VO
	private String actionPath;									//操作成功跳转路径
	
	/**展示所有渠道扣量配置信息*/
	public String getConfigFilterList(){
		List<ConfigFilterVo> configFilterVoList = configFilterDao.getConfigFilterList();
		if(configFilterVoList != null && configFilterVoList.size() > 0){
			Map<String, List<ConfigFilterVo>> configFilterVoListMap = new LinkedHashMap<String, List<ConfigFilterVo>>();
			for(ConfigFilterVo configFilterVo : configFilterVoList){
				int lower_id = configFilterVo.getLower_id();
				String lower_name = configFilterVo.getLower_name();
				String province = configFilterVo.getProvince();
				String key = lower_id + ":" + lower_name + ":" + province;
				
				if(configFilterVoListMap.get(key) != null){
					configFilterVoListMap.get(key).add(configFilterVo);
				}else{
					List<ConfigFilterVo> configFilterVoListTemp = new ArrayList<>();
					configFilterVoListTemp.add(configFilterVo);
					configFilterVoListMap.put(key, configFilterVoListTemp);
				}
			}
			
			/**遍历Map 把键值放入一个VO里*/
			if(configFilterVoListMap != null && configFilterVoListMap.size() > 0){
				configFilterMapVoList = new ArrayList<ConfigFilterMapVo>();
				for (Map.Entry<String, List<ConfigFilterVo>> entry : configFilterVoListMap.entrySet()) {
					String[] keys = entry.getKey().split(":");
					int lower_id = Integer.parseInt(keys[0]);
					String lower_name = keys[1];
					String province = "";
					if(keys.length > 2){
						province = keys[2];
					}
					ConfigFilterMapVo configFilterMapVo = new ConfigFilterMapVo();
					configFilterMapVo.setLower_id(lower_id);
					configFilterMapVo.setLower_name(lower_name);
					configFilterMapVo.setProvince(province);
					configFilterMapVo.setConfigFilterVoList(entry.getValue());
					
					configFilterMapVoList.add(configFilterMapVo);
				}
			}
		}else{
			configFilterMapVoList = null;
		}
		return SUCCESS;
	}
	
	/**新增渠道扣量配置信息*/
	public String addConfigFilter(){
		configFilterVo = null;
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		return SUCCESS;
	}
	
	/**保存新增扣量信息*/
	public String saveConfigFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		if(configFilterVo.getLower_id() != 0){
			try {
				configFilterVo.setProvince(URLDecoder.decode(configFilterVo.getProvince(), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				logger.error(String.format("保存新增扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
			}
			
			/**分割所有省份*/
			String[] provinces = configFilterVo.getProvince().split(":");
			/**根据渠道ID获得渠道下的长号码列表*/
			List<String> sp_number_list = configLowerPartnerDao.getLowerPartnerSpNumberById(configFilterVo.getLower_id());
			
			if(sp_number_list != null && sp_number_list.size() > 0){
				List<ConfigFilterVo> configFilterList = new ArrayList<ConfigFilterVo>();
				for(String sp_number : sp_number_list){
					if(provinces != null && provinces.length > 0){
						for(String province : provinces){
							ConfigFilterVo configFilter = new ConfigFilterVo();
							configFilter.setLower_id(configFilterVo.getLower_id());
							configFilter.setSp_number(sp_number);
							configFilter.setProvince(province);
							configFilter.setNum(configFilterVo.getNum());
							configFilter.setStart_time(configFilterVo.getStart_time());
							configFilter.setEnd_time(configFilterVo.getEnd_time());
							
							configFilterList.add(configFilter);
						}
					}
				}
				
				if(configFilterList != null && configFilterList.size() > 0){
					/**批量保存渠道扣量信息*/
					int count = configFilterDao.batchInsertConfigFilter(configFilterList);
					if(count > 0){
						logger.info(String.format("[%s]新增[%s]渠道扣量信息成功", userInfoVo.getLogin_username(),configFilterVo.getProvince()));
					}else{
						logger.info(String.format("[%s]新增[%s]渠道扣量信息失败", userInfoVo.getLogin_username(),configFilterVo.getProvince()));
					}
				}
			}
			
		}
		actionPath = "configFilter/getConfigFilterList.action";
		return SUCCESS;
	}
	
	public String modifyLowerConfigFilter(){
		try {
			/**本地测试*/
//			String province = new String(configFilterVo.getProvince().getBytes("ISO-8859-1"),"UTF-8");
//			configFilterVo.setProvince(province);
			
			/**线上环境*/
			configFilterVo.setProvince(URLDecoder.decode(configFilterVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("修改渠道扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(configFilterVo.getLower_id() != 0){
			configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		}
		
		return SUCCESS;
	}
	
	public String updateLowerConfigFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		try {
			configFilterVo.setProvince(URLDecoder.decode(configFilterVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("修改渠道扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(configFilterVo.getLower_id() != 0){
			int count = configFilterDao.updateLowerConfigFilter(configFilterVo);
			if(count > 0){
				logger.info(String.format("[%s]更新渠道扣量[%s]管控配置成功", userInfoVo.getLogin_username(),configFilterVo.getProvince()));
			}else{
				logger.info(String.format("[%s]更新渠道扣量[%s]管控配置失败", userInfoVo.getLogin_username(),configFilterVo.getProvince()));
			}
		}
		actionPath = "configFilter/getConfigFilterList.action";
		return SUCCESS;
	}
	
	public String modifyConfigFilter(){
		if(configFilterVo.getId() != 0){
			configFilterVo = configFilterDao.getConfigFilterById(configFilterVo.getId());
		}
		
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		
		return SUCCESS;
	}
	
	public String updateConfigFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		try {
			configFilterVo.setProvince(URLDecoder.decode(configFilterVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("保存长号码扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(configFilterVo.getId() != 0){
			int count  = configFilterDao.updateConfigFilter(configFilterVo);
			if(count > 0){
				logger.info(String.format("[%s]更新长号码扣量[%s]配置成功", userInfoVo.getLogin_username(),configFilterVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]更新长号码扣量[%s]配置失败", userInfoVo.getLogin_username(),configFilterVo.getSp_number()));
			}
		}
		
		actionPath = "configFilter/getConfigFilterList.action";
		return SUCCESS;
	}
	
	public String deleteConfigFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		try {
			configFilterVo.setProvince(URLDecoder.decode(configFilterVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除长号码扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		int count = configFilterDao.deleteConfigFilter(configFilterVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除长号码[%s][%s]省扣量信息成功", platUser.getLogin_username(),configFilterVo.getSp_number(),configFilterVo.getProvince()));
		}else{
			logger.info(String.format("[%s]删除长号码[%s][%s]省扣量信息失败", platUser.getLogin_username(),configFilterVo.getSp_number(),configFilterVo.getProvince()));
		}
		
		actionPath = "configFilter/getConfigFilterList.action";
		return SUCCESS;
	}
	
	public String deleteLowerConfigFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		try {
			configFilterVo.setProvince(URLDecoder.decode(configFilterVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除长号码省份管控信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		int count = configFilterDao.deleteLowerConfigFilter(configFilterVo.getLower_id(), configFilterVo.getProvince());
		if(count > 0){
			logger.info(String.format("[%s]删除渠道[%s]扣量信息成功", platUser.getLogin_username(),configFilterVo.getProvince()));
		}else{
			logger.info(String.format("[%s]删除渠道[%s]扣量信息失败", platUser.getLogin_username(),configFilterVo.getProvince()));
		}
		
		actionPath = "configFilter/getConfigFilterList.action";
		return SUCCESS;
	}
	
	public IConfigFilterDao getConfigFilterDao() {
		return configFilterDao;
	}
	public void setConfigFilterDao(IConfigFilterDao configFilterDao) {
		this.configFilterDao = configFilterDao;
	}
	public IConfigLowerPartnerDao getConfigLowerPartnerDao() {
		return configLowerPartnerDao;
	}
	public void setConfigLowerPartnerDao(IConfigLowerPartnerDao configLowerPartnerDao) {
		this.configLowerPartnerDao = configLowerPartnerDao;
	}
	public List<ConfigFilterMapVo> getConfigFilterMapVoList() {
		return configFilterMapVoList;
	}
	public void setConfigFilterMapVoList(List<ConfigFilterMapVo> configFilterMapVoList) {
		this.configFilterMapVoList = configFilterMapVoList;
	}
	public List<ConfigLowerPartnerVo> getConfigLowerPartnerVoList() {
		return configLowerPartnerVoList;
	}
	public void setConfigLowerPartnerVoList(List<ConfigLowerPartnerVo> configLowerPartnerVoList) {
		this.configLowerPartnerVoList = configLowerPartnerVoList;
	}
	public ConfigFilterVo getConfigFilterVo() {
		return configFilterVo;
	}
	public void setConfigFilterVo(ConfigFilterVo configFilterVo) {
		this.configFilterVo = configFilterVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
